package com.kedacom.emptyproject.view.activity

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import com.kedacom.emptyproject.BR
import com.kedacom.emptyproject.R
import com.kedacom.emptyproject.adapter.SimpleAdapter
import com.kedacom.emptyproject.databinding.ActivitySimpleBinding
import com.kedacom.emptyproject.util.DateUtils
import com.kedacom.emptyproject.viewmodel.SimpleViewModel
import com.kedacom.util.LegoLog
import com.kedacom.widget.pop.DateTimePickerDialog
import com.kedacom.widget.pop.listener.OnDateTimePickListener
import java.util.*


/**
 * 显示当前月份的日历（精简版）
 *
 * 更丰富的功能，请参考 MainActivity
 */
class SimpleActivity : BaseActivity<ActivitySimpleBinding, SimpleViewModel>() {

    override fun getContentViewId() = R.layout.activity_simple

    private lateinit var mActivity: Activity
    private var scheduleItemAdapter: SimpleAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mActivity = this
        initView()
    }

    private fun initView() {
        nViewDataBinding.llTaskCalendarDateSelect.setOnClickListener {
            showDateSelectedDialog()
        }

        // 初始年月
        val year: Int = Calendar.getInstance().get(Calendar.YEAR)
        val month: Int = Calendar.getInstance().get(Calendar.MONTH) + 1

        nViewDataBinding.tvTaskScheduleCalendarYear.text = year.toString()
        nViewDataBinding.tvTaskScheduleCalendarMonth.text = month.toString()


        scheduleItemAdapter =
            SimpleAdapter(
                R.layout.item_simple_day,
                null,
                BR.itemScheduleBean
            )
        nViewDataBinding.rvItemSchedule.layoutManager = GridLayoutManager(context, 7)
        nViewDataBinding.rvItemSchedule.adapter = scheduleItemAdapter
        scheduleItemAdapter!!.data = converData(year, month)
    }

    /**
     * 将服务端返回值转换为我们需要的实体对象
     * @param year Int
     * @param month Int
     * @param list List<ResultBean>
     * @return ArrayList<ScheduleItemInfo>
     */
    private fun converData(
        year: Int,
        month: Int
    ): MutableList<Int> {
        LegoLog.d("选中的年=$year,月=$month")
        val days: Array<IntArray> = DateUtils.getDayOfMonthFormat(year, month)//获取当前月份的日历分布

        val rows = if (days[days.size - 1][0] < 25) {//最后一行的首位日期如果小于25，就不需要换行。
            5 * 7//5周，每周7天
        } else {
            6 * 7//6周，每周7天
        }

        val dataList = mutableListOf<Int>()
        var dayNum = 0
        //将二维数组转化为一维数组，方便使用
        for (i in days.indices) {
            for (element in days[i]) {
                if (dayNum >= rows) {
                    break
                }
                dataList.add(element)
                dayNum++
            }
        }
        return dataList
    }

    /**
     * 显示日期选择弹窗
     */
    private fun showDateSelectedDialog() {
        val dateTimePickerDialog: DateTimePickerDialog = DateTimePickerDialog.Builder()
            .title("选择时间")
            .optionsViewShowConfig(
                showYear = true, showMonth = true,
                showDay = false, showHour = false, showMinute = false
            )
            // 显示的时间单位名称，默认为 年 月 日，不显示"时"和"分"
            .onDateTimePickListener(object : OnDateTimePickListener {

                override fun onDateTimePick(date: Date) {
                    val calendar = Calendar.getInstance()
                    calendar.time = date
                    val year = calendar.get(Calendar.YEAR)
                    val month = calendar.get(Calendar.MONTH) + 1
                    nViewDataBinding.tvTaskScheduleCalendarYear.text = year.toString()
                    nViewDataBinding.tvTaskScheduleCalendarMonth.text = month.toString()
//                    showToast("选中的时间为" + DatetimeUtil.dateToLongStr(date))
                    // 调用接口查询选中年的月份用户每天的任务列表。


                    scheduleItemAdapter!!.data = converData(year, month)
                }
            })
            .build()

        dateTimePickerDialog.show(this)
    }

}
